<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>在字符串所有新行之前插入 HTML 换行标记</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.nl-langinfo.html">« nl_langinfo</a></li>
      <li style="float: right;"><a href="function.number-format.html">number_format »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>在字符串所有新行之前插入 HTML 换行标记</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.nl2br" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">nl2br</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">nl2br</span> &mdash; <span class="dc-title">在字符串所有新行之前插入 HTML 换行标记</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.nl2br-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>nl2br</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_xhtml</code><span class="initializer"> = <strong><code>true</code></strong></span></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   在字符串 <code class="parameter">string</code> 所有新行之前插入 <code class="code">&#039;&lt;br /&gt;&#039;</code> 或 <code class="code">&#039;&lt;br&gt;&#039;</code>，并返回。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.nl2br-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       输入字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">use_xhtml</code></dt>

     <dd>

      <p class="para">
       是否使用 XHTML 兼容换行符。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.nl2br-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回调整后的字符串。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.nl2br-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4788">
    <p><strong>示例 #1 <span class="function"><strong>nl2br()</strong></span> 使用范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #DD0000">"foo&nbsp;isn't\n&nbsp;bar"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
foo isn&#039;t&lt;br /&gt;
 bar
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4789">
    <p><strong>示例 #2 使用 <code class="parameter">use_xhtml</code> 生成合法的 HTML 标记</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #DD0000">"Welcome\r\nThis&nbsp;is&nbsp;my&nbsp;HTML&nbsp;document"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Welcome&lt;br&gt;
This is my HTML document
</pre></div>
    </div>
   </div>
   
   <div class="example" id="example-4790">
    <p><strong>示例 #3 各种换行分隔符</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"This\r\nis\n\ra\nstring\r"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">nl2br</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
This&lt;br /&gt;
is&lt;br /&gt;
a&lt;br /&gt;
string&lt;br /&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.nl2br-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - 将特殊字符转换为 HTML 实体</span></li>
    <li class="member"><span class="function"><a href="function.htmlentities.html" class="function" rel="rdfs-seeAlso">htmlentities()</a> - 将字符转换为 HTML 转义字符</span></li>
    <li class="member"><span class="function"><a href="function.wordwrap.html" class="function" rel="rdfs-seeAlso">wordwrap()</a> - 打断字符串为指定数量的字串</span></li>
    <li class="member"><span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - 子字符串替换</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>